package com.xysq.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.AddressModel;
import com.rockcent.model.CustomerBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import butterknife.InjectView;

public class ConfirmExpressActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.btn_confirm_express)
    Button confirmExpressBtn;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.txt_customer_name)
    TextView nameTxt;
    @InjectView(R.id.txt_number)
    TextView numberTxt;
    @InjectView(R.id.btn_change_express_info)
    Button changeExpressInfoBtn;
    @InjectView(R.id.txt_address)
    TextView addressTxt;
    @InjectView(R.id.txt_addressee)
    TextView addresseeTxt;
    @InjectView(R.id.txt_linkphone)
    TextView linkphoneTxt;
    @InjectView(R.id.edit_remark)
    EditText remarkEdit;
    CustomerBO customerBO;

    String name;
    int count;
    int verificationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_express);
        getExtra();
        initViewAndData();
        loadData();
//        loadData();
    }

    private void loadData() {
        appAction.getCustomer(UserInfoKeeper.readPhoneNumber(ConfirmExpressActivity.this), "", "", new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                addressTxt.setText("姓名：" + data.getName());
                addresseeTxt.setText("地址：" + data.getAddress());
                linkphoneTxt.setText("联系电话：" + data.getPhone());
                customerBO = data;
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(ConfirmExpressActivity.this, "获取个人信息失败");
            }
        });
    }

    private void getExtra() {
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        count = intent.getIntExtra("count", 0);
        verificationId = intent.getIntExtra("verificationId", 0);
    }

    private void initViewAndData() {
        changeExpressInfoBtn.setOnClickListener(this);
        confirmExpressBtn.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
        nameTxt.setText(name);
        numberTxt.setText("兑换数量: " + String.valueOf(count));
    }

    @Override
    public void onClick(View view) {
        if (view == confirmExpressBtn) {
            if (customerBO != null) {
                if (TextUtils.isEmpty(customerBO.getName()) | TextUtils.isEmpty(customerBO.getAddress())) {
                    ToastUtil.showShort(ConfirmExpressActivity.this, "请设置收货信息");
                } else {
                    confirmExpressBtn.setEnabled(false);
                    int schoolId;
                    try {
                        schoolId = Integer.valueOf(customerBO.getSchoolId());
                    } catch (Exception e) {
                        ToastUtil.showShort(this, "获取学校信息失败");
                        confirmExpressBtn.setEnabled(true);
                        return;
                    }
                    appAction.getPositonBySchool(schoolId, new CallbackListener<AddressModel>() {
                        @Override
                        public void onSuccess(AddressModel data) {
                            appAction.startVerificationDelivery(verificationId, Integer.valueOf(data.getProvinceID()), "", Integer.valueOf(data.getCityID()), "", Integer.valueOf(data.getLocationID()), "", customerBO.getAddress(), customerBO.getName(), UserInfoKeeper.readPhoneNumber(ConfirmExpressActivity.this), remarkEdit.getText().toString(), new CallbackListener<String>() {
                                @Override
                                public void onSuccess(String data) {
                                    dialog_confirm_express();
                                    confirmExpressBtn.setEnabled(true);
                                }

                                @Override
                                public void onFailure(String errorCode, String message) {
                                    confirmExpressBtn.setEnabled(true);
                                        ToastUtil.showLong(ConfirmExpressActivity.this, R.string.toast_express_fail);
                                }
                            });
                        }

                        @Override
                        public void onFailure(String errorCode, String message) {
                            ToastUtil.showLong(ConfirmExpressActivity.this, R.string.toast_get_school_position_fail);
                        }
                    });
                }
            }
        }
        if (view == backIbtn) {
            finish();
        }
        if (view == changeExpressInfoBtn) {
            MobclickAgent.onEvent(this, "1023");
            Intent intent = new Intent(ConfirmExpressActivity.this, UserActivity.class);
            startActivity(intent);
        }
    }

    private void dialog_confirm_express() {
        //先new出一个监听器，设置好监听
        DialogInterface.OnClickListener
                dialogOnclicListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case Dialog.BUTTON_POSITIVE:
                        Intent intent = new Intent(ConfirmExpressActivity.this, MyTicketsActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        };
        //dialog参数设置
        AlertDialog.Builder builder = new AlertDialog.Builder(ConfirmExpressActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);  //先得到构造器
        builder.setTitle(null); //设置标题
        builder.setMessage("请求已收到，我们将速速安排快递"); //设置内容
        builder.setPositiveButton("返回券包", dialogOnclicListener);
        builder.create().show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("确认快递页");
        MobclickAgent.onResume(this);
        loadData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("确认快递页");
        MobclickAgent.onPause(this);
    }
}
